package com.cc.data_explore.gui.importfile;

import com.cc.data_explore.logic.utils.ViewModelListCellFactory;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

/**
 * ImportDialogView
 *
 * @author aa
 * @version 1.0
 * @since 2025/3/27
 */
public class ImportFileDialogView implements FxmlView<ImportFileDialogViewModel> {
    @FXML
    public ListView<String> sheetList;
    @FXML
    public TextField filePath;
    @FXML
    public ComboBox<Delimiter> csvDelimiter;
    @FXML
    public Button browseFileBtn;
    @FXML
    public Button saveDataClick;
    @FXML
    public HBox csvTipBox;
    @FXML
    public Button readFileBtn;

    @InjectViewModel
    private ImportFileDialogViewModel viewModel;

    @FXML
    public void initialize() {
        sheetList.itemsProperty().bind(viewModel.sheetListProperty());
        filePath.textProperty().bindBidirectional(viewModel.filePathProperty());
        csvDelimiter.itemsProperty().bind(viewModel.csvDelimiterProperty());
        saveDataClick.disableProperty().bind(viewModel.saveBtnProperty());
        readFileBtn.disableProperty().bind(viewModel.readBtnProperty());
        csvTipBox.visibleProperty().bind(viewModel.visibleProperty());
        csvDelimiter.valueProperty().bindBidirectional(viewModel.selectedDelimiterProperty());
        new ViewModelListCellFactory<Delimiter>().withText(viewModel::getText).install(csvDelimiter);

    }

    public void readFile(ActionEvent actionEvent) {
        viewModel.readFile(filePath.getText());
    }

    public void saveData(ActionEvent actionEvent) {
        viewModel.saveData();

        // tabViewModel.buildFileTree();
    }

    public void browseFile(ActionEvent actionEvent) {
        viewModel.browse();
    }
}
